<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/15
 * Time: 12:43
 */

namespace frontend\modules\v1\controllers;
use frontend\controllers\BaseController;
use Yii;
use frontend\modules\v1\models\BusinessForm;
use common\models\Business;

/**
 * Class BusinessController
 * @package frontend\modules\v1\controllers
 */
class BusinessController extends BaseController {

    /**
     * 添加商家其他信息接口
     * @param name 商家名称
     * @param phone 联系方式
     * @param logo logo图标路径
     * @param type 商家类型 1影楼 2工作室
     * @param province 省份
     * @param city 市
     * @param area 区
     * @param address 详细地址
     * @param location 地图坐标
     * @param licence 营业执照
     * @param status 状态
     * @param auth_key
     * @param id
     * @param url 商家链接
     */
    public function actionAdd()
    {
        /** 校验表单是否为空 */
        if(!$this->postData){
            return $this->responseError("参数信息为空.");
        }
        /** 校验auth_key */
        if(empty($this->postData['auth_key'])){
            return $this->responseError('auth_key is null');
        }
        /** 定义返回数据 */
        $resultData = array();
        /** 接收数据 */
        $businessForm = new BusinessForm();
        $businessForm->auth_key = $this->postData['auth_key'];
        $businessForm->phone = $this->postData['phone'];
        $businessForm->logo = $this->postData['logo'];
        $businessForm->type = $this->postData['type'];
        $businessForm->province = $this->postData['province'];
        $businessForm->city = $this->postData['city'];
        $businessForm->area = $this->postData['area'];
        $businessForm->address = $this->postData['address'];
        $businessForm->location = $this->postData['location'];
        $businessForm->licence = $this->postData['licence'];
        $businessForm->name = $this->postData['name'];
        $businessForm->status = $this->postData['status'];
        $businessForm->url = isset($this->postData['url']) ? $this->postData['url'] : '';

        /** 添加 */
        $bool = $businessForm->add();
        error_log("businessAdd_bool = " . $bool);
        if($bool){
            /** 成功返回 */
            $resultData['id'] = $businessForm->getBusiness()->getId();
            $resultData['auth_key'] = $businessForm->getBusiness()->getAuthKey();
            return $this->responseFormat($resultData);
        }

        /** 失败返回 */
        if ($businessForm->errors) {
            foreach ($businessForm->errors as $key => $info) {
                $resultData[$key] = $info;
            }
        }
        return $this->responseError($resultData);
    }

    /**
     * 查询商家信息接口
     */
    public function actionView()
    {
        /** 验证身份 */
        $isGuest = Yii::$app->user->getIsGuest();
        /** 未登陆 */
        if($isGuest){
            return $this->responseError('当前用户未登陆.');
        }
        /** 返回信息 */
        $resultData = array();
        /** 已登录 */
        $id = Yii::$app->user->getId();
        /** 查询商家信息 */
        $business = Business::findIdentity($id);
        if(!$business){
            return $this->responseFail('数据已经丢失');
        }

        $resultData['detail'] = $business;
        return $this->responseFormat($resultData);
    }
}